<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px">患者</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px">电话</label>
                            <div class="layui-input-inline">
                                <input type="text" name="tel" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px">状态</label>
                            <div class="layui-input-inline">
                                <select name="status">
                                     <option value="">请选择</option>
                                     <option value="0">未就诊</option>
                                    <option value="1">已就诊</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-blue layui-btn-normal" lay-event="edit">就诊诊断</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'axios'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var axios = layui.axios;
     //重新加载 table
        window.reload=function(){
            table.reload('currentTableId');
        }

        form.render();

        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:9999/order/queryOrderInfoAll2',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: 'ID', sort: true},
                {templet:'<div>{{ d.patientInfo.username}}</div>', width: 100, title: '患者姓名'},
                {templet:'<div>{{ d.patientInfo.tel}}</div>', width: 100, title: '患者电话'},
                {field: 'day', width: 100, title: '预约时间'},
                {field: 'dayTime',width: 120, title: '时间段',templet:function(d){
                        if(d.dayTime=='am'){
                            return '<span class="layui-badge layui-bg-green" >上午</span>';
                        }else{
                            return '<span class="layui-badge  layui-bg-blue">下午</span>';
                        }
                    }},
                {templet:'<div>{{ d.user.username}}</div>', width: 100, title: '预约医生'},
                {width: 80, title: '就诊状态',templet:function(d){
                        if(d.status==1){
                            return '<span class="layui-badge layui-bg-green" >已就诊</span>';
                        }else{
                            return '<span class="layui-badge  layui-bg-blue">未就诊</span>';
                        }
                    }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var username=data.field.username;
            var tel=data.field.tel;
            var status=data.field.status;
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    username: username,
                    tel:tel,
                    status:status
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
           if (obj.event === 'delete') {  // 监听删除操作
                //批量删除功能
                /*
                  1、获取要删除的记录信息 ，如果没选择 提示
                  2、获取要删除记录的id信息
                  3、监听提交事件调用后端方法
                 */
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length==0){
                    layer.msg("请选择你要删除的记录信息");
                }else{
                    //调用获取选中记录的id
                    var ids=getCheackId(data);
                    layer.confirm('真的删除行么', function (index) {
                        //像后端调用请求
                        axios.get('order/deleteByIds?ids='+ids).then(function(response){
                            if(response.data.code==200){
                                layer.msg(response.data.msg);
                                //重新加载表格
                                // window.reload();
                                table.reload('currentTableId');
                            }
                        }).catch(err=> {
                                console.log(err);
                            }
                        );

                    });
                }

            }
        });

        /**
         * 获取记录信息的id
         */
        function getCheackId(data){
            var arr=new Array();
            for(var i=0;i<data.length;i++){
                arr.push(data[i].id);
            }
            return arr.join(",");
        }


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        //渲染数据 编辑
        function setFormValues(data){
            form.val('saveBtn',{
                "userId":data.userId,
                "patientId":data.patientId
            });
        }
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            debugger
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('page/other/visitAdd.html?id='+data.id);
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '就诊记录',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                 // $("#userID").val(data.id)
                 //渲染值信息
                 setFormValues(data);
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //获取要删除的id 调用相关后端接口即可
                    axios.get('order/deleteByIds?ids='+data.id).then(function(response){
                        if(response.data.code==200){
                            layer.msg(response.data.msg);
                            //重新加载表格
                            // window.reload();
                            table.reload('currentTableId');
                        }
                    }).catch(err=> {
                            console.log(err);
                        }
                    );


                });
            }
        });

    });
</script>